package com.bootdo.common.controller;

import com.bootdo.system.domain.UserToken;
import org.springframework.stereotype.Controller;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BaseController {
	public UserDO getUser() {
		return ShiroUtils.getUser();
	}

	public Long getUserId() {
		return getUser().getUserId();
	}

	public String getUsername() {
		return getUser().getUsername();
	}

	/**
	 * 获取域名url(http://...:.../)
	 * @return
	 */
	public String getDomainUrl() {
		HttpServletRequest request = this.getRequest();
		String domainUrl = request.getRequestURL().toString();
		String contextName = request.getContextPath();
		int index = domainUrl.indexOf(contextName);
		domainUrl=domainUrl.substring(0, index);
		return domainUrl;
	}

	/**
	 * 获取域名url(http://...:.../ProjectName)
	 * @return
	 */
	public String getDomainPath(){
		HttpServletRequest request = this.getRequest();
		return this.getDomainUrl() + request.getContextPath();
	}

	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 获取服务器地址，端口号
	 * @return
	 */
	public String getServerIpAndPort(){
		HttpServletRequest request = this.getRequest();
		String serverIp = request.getServerName();
		int port = request.getServerPort();
		return serverIp + ":" + port;
	}

}